package com.xdtech.hotel.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.TestDataDao;
import com.xdtech.hotel.model.TestData;
import com.xdtech.hotel.service.TestDataService;
import com.xdtech.hotel.vo.TestDataItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-06-05 14:18:20
 * @since 1.0
 * @see
 */
@Service
public class TestDataServiceImpl implements TestDataService {
	private Log log = LogFactory.getLog(TestDataServiceImpl.class);
	@Autowired
	private TestDataDao testDataDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @param entity
	 */
	public void save(TestData entity) {
		testDataDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<TestData> entities) {
		testDataDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @param entity
	 */
	public void delete(TestData entity) {
		testDataDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		testDataDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @param id
	 * @return
	 */
	public TestData get(Long id) {
		return testDataDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @return
	 */
	public List<TestData> getAll() {
		return testDataDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> testDatas = null;
		long rows = 0;
		if (pg!=null) {
			Page<TestData> page = testDataDao.findPage(pg,"from TestData order by createTime desc", values);
			testDatas = BeanUtils.copyListProperties(
					TestDataItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<TestData> testDataList = testDataDao.find("from TestData order by id desc", values);
			testDatas = BeanUtils.copyListProperties(
					TestDataItem.class, testDataList);
			rows = testDatas.size();
		}
		results.put("total", rows);
		results.put("rows", testDatas);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateTestData(TestDataItem item) {
		TestData testData = null;
		if (item.getId()==null) {
			testData = new TestData();
		}else {
			testData = testDataDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(testData, item);
		testDataDao.save(testData);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @param testDataId
	 * @return
	 */
	public TestDataItem loadTestDataItem(Long testDataId) {
		TestData testData = testDataDao.get(testDataId);
		return TestDataItem.createItem(testData);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteTestDataInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteTestDataInfo(List<Long> testDataIds) {
		for (Long id : testDataIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-05 14:18:20
	 * @modified by
	 * @return
	 */
	@Override
	public List<TestDataItem> loadItems() {
		List<TestData> testDatas = getAll();
		return TestDataItem.createItems(testDatas);
	}
	/* (非 Javadoc)
	 * getReturnData
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param postContent
	 * @return
	 * @see com.xdtech.hotel.service.TestDataService#getReturnData(java.lang.String)
	 */
	@Override
	public String getReturnData(String postContent)
	{
		List<TestData> testDatas = testDataDao.findByHql("from TestData where receData=?", postContent);
		if (EmptyUtil.isNotEmpty(testDatas))
		{
			return testDatas.get(0).getReturnData();
		}else
		{
			return "";
		}
	}

}
